package com.gxuwz.Market.business.dao;

import org.springframework.stereotype.Repository;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gxuwz.Market.business.entity.ServiceRecord;
import com.gxuwz.core.dao.impl.BaseDaoImpl;
import com.gxuwz.core.pagination.Result;



@Repository("serviceRecordDAO")
public class ServiceRecordDAO extends BaseDaoImpl<ServiceRecord>{
	/**
	 * 根据条件查找分页
	 * @param serviceRecord 模型
	 * @param page 第几页
	 * @param row 长度
	 * @return
	 * @author 李宪乾
	 * @date 2017.9.27
	 */
	@SuppressWarnings("unchecked")
	public Result<ServiceRecord> find(ServiceRecord serviceRecord, int page, int size){
		String queryString = "from ServiceRecord where 1=1";
		if(null != serviceRecord.getId() && !"".equals(serviceRecord.getId())){			
			queryString = queryString + "and Id like '%"+serviceRecord.getId()+"%'";
		}
		if(null != serviceRecord.getWriterId() && !"".equals(serviceRecord.getWriterId())){
			queryString = queryString +" and writerId like '%"+serviceRecord.getWriterId()+"%'";
		}
		if(null != serviceRecord.getFormId() && !"".equals(serviceRecord.getFormId())){
			queryString = queryString +" and formId like '%"+serviceRecord.getFormId()+"%'";
		}
		if(null != serviceRecord.getEditTime() && !"".equals(serviceRecord.getEditTime())){
			queryString = queryString +" and editTime like '%"+serviceRecord.getEditTime()+"%'";
		}
		if(null != serviceRecord.getFormName() && !"".equals(serviceRecord.getFormName())){
			queryString = queryString +" and formName like '%"+serviceRecord.getFormName()+"%'";
		}
		if(null != serviceRecord.getDataName() && !"".equals(serviceRecord.getDataName())){
			queryString = queryString +" and dataName like '%"+serviceRecord.getDataName()+"%'";
		}
		queryString = queryString + " order by editTime desc";
		int start=(page-1)*size;
		int limit =size;
		return (Result<ServiceRecord> )super.find(queryString, null, null, start, limit);
	}
	
	public List<ServiceRecord> findAll() {
		List<ServiceRecord> serviceClassList = null;
		String queryString = "from ServiceRecord where 1=1";
		serviceClassList = this.getHibernateTemplate().find(queryString);
		return serviceClassList;
	}
	/**
	 * 根据id查询单个用户
	 * @param userId
	 * @return
	 */
	public ServiceRecord findById(int id){
		List<ServiceRecord> serviceClassList = null;
		String queryString = "from ServiceRecord where id ="+id;
		try{
			serviceClassList = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return serviceClassList.get(0);
	}
	
	public List<ServiceRecord> findByProperty(String property, String value){
		List<ServiceRecord> list = null;
		String queryString = "from ServiceRecord where "+property+" ='"+value+"' ";
		try{
			list = this.getHibernateTemplate().find(queryString);
		}catch(Exception e){
			System.out.println(e);
		}
		return list;
	}
}